package com.xumt.hwHMSdemo.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xumt.hwHMSdemo.Login2;
import com.xumt.hwHMSdemo.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommodityAdapter extends RecyclerView.Adapter<CommodityAdapter.ViewHolder> {
    private List<String>disrciptionlist;
    private List<String>commdity_pricelist;
    private Context context;
    private List<Map<String,Object>>Shopgoing=new ArrayList<>();
    public static int allprice=0;
    public static CheckBox checkBox1;
    public static List<String>buylist=new ArrayList<>();
    public static List<String>numberlist=new ArrayList<>();
    public CommodityAdapter(Context context,List<Map<String,Object>> Shopgoing)
    {
        this.context=context;
        this.Shopgoing=Shopgoing;
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name;
        private ImageView imageView;
        private TextView discription;
        private TextView commdity_price;
        private RecyclerView recyclerView;
        private TextView amont;
        private Button Addbutton;
        private Button downbutton;
        private CheckBox checkBox;
        private TextView Delete_view;
        public ViewHolder(View view)
        {
            super(view);
            name=(TextView)view.findViewById(R.id.gogogo);
            imageView=(ImageView)view.findViewById(R.id.commodity_card);
            discription=(TextView)view.findViewById(R.id.discription_list);
            commdity_price=(TextView)view.findViewById(R.id.commdity_price);
            recyclerView=(RecyclerView)view.findViewById(R.id.commodityview);
            amont=(TextView)view.findViewById(R.id.amont);
            Addbutton=(Button)view.findViewById(R.id.Addbutton);
            downbutton=(Button)view.findViewById(R.id.downbutton);
            checkBox=(CheckBox)view.findViewById(R.id.chancebox);
            Delete_view=(TextView)view.findViewById(R.id.Deleteview);

        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.name.setText(String.valueOf(Shopgoing.get(position).get("name")) );
        holder.discription.setText(String.valueOf(Shopgoing.get(position).get("discription")));
        holder.imageView.setImageResource((Integer) Shopgoing.get(position).get("image"));
        holder.commdity_price.setText(String.valueOf(Shopgoing.get(position).get("price")));
        holder.amont.setText(String.valueOf(Shopgoing.get(position).get("number")));

        holder.Addbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Shopgoing.get(position).put("number",Integer.parseInt(String.valueOf(holder.amont.getText()))+1);
                if (holder.checkBox.isChecked())
                {
                    allprice+=Integer.parseInt(String.valueOf(holder.commdity_price.getText()));
                }
                notifyDataSetChanged();
            }
        });

        holder.downbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(String.valueOf(holder.amont.getText()))==1)
                {
                    Toast.makeText(context, "你至少得买一个吧！" ,Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Shopgoing.get(position).put("number",Integer.parseInt(String.valueOf(holder.amont.getText()))-1);
                    if (holder.checkBox.isChecked())
                    {
                        allprice-=Integer.parseInt(String.valueOf(holder.commdity_price.getText()));
                    }
                    notifyDataSetChanged();
                }

            }
        });

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.checkBox.isChecked())
                {
                    Map<String,Object> map=new HashMap<String,Object>();
                    allprice+=Integer.parseInt(String.valueOf(holder.amont.getText()))*Integer.parseInt(String.valueOf(holder.commdity_price.getText()));

                    buylist.add(String.valueOf(holder.name.getText()));
                    numberlist.add(String.valueOf(holder.amont.getText()));
                    Log.i("allrpece","prece"+allprice);
//                    Log.i("allrpece","map"+map.get("ordid"));
                    Log.i("allrpece","list"+buylist);
                    Log.i("allrpece","list"+numberlist);
                }
                else {
                    allprice-=Integer.parseInt(String.valueOf(holder.amont.getText()))*Integer.parseInt(String.valueOf(holder.commdity_price.getText()));
                    buylist.remove(buylist.size()-1);
                    Log.i("allrpece","prece"+allprice);

                }
            }
        });
        holder.Delete_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Shopgoing.remove(position);
                notifyDataSetChanged();
            }
        });


    }

    @Override
    public int getItemCount() {
        return Shopgoing.size();
    }



}
